package com.maben.udp.test001;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 *udp通信发送端
 */
public class UdpSender {
    private static AtomicLong num = new AtomicLong(0);
    public static void main(String[] args)throws Exception {
        DatagramSocket datagramSocket = new DatagramSocket();
        String s = "110000000012000000002hello,world!";
        byte[] bytes1 = HexUtil.decodeHex("aa 00 23 06 74 72 61 69 6e 31 31 30 30 31 64 47 73 68 01 6f 10 02 00 0c 03 00 01 00 03 03 4c f5 20 00 0a 0d 10 03 ");
        byte[] bytes2 = HexUtil.decodeHex("aa 00 63 06 74 72 61 69 6e 31 31 30 30 31 64 47 73 68 01 63 10 02 00 4c 06 00 01 00 f7 05 24 47 50 52 4d 43 2c 30 31 32 38 33 30 2e 30 30 2c 41 2c 32 35 35 38 2e 36 35 39 39 38 2c 4e 2c 31 31 37 32 32 2e 34 32 32 30 31 2c 45 2c 30 2e 30 30 36 2c 2c 31 32 30 33 32 33 2c 2c 2c 41 2a 37 43 0d 0a 1d 05 10 03 ");
        List<byte[]> list = new ArrayList<byte[]>(){{
            add(bytes1);
            add(bytes2);
        }};
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 30002;
        for (int i = 0; i < 1; i++) {
            byte[] bytes = list.get(NumberUtil.generateRandomNumber(0, 2, 1)[0]);
            TimeUnit.MILLISECONDS.sleep(1000);
            System.out.println("send msg:"+num.addAndGet(1));
            DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, address, port);
            datagramSocket.send(datagramPacket);
        }
    }
}
